package com.project.controller;

import com.github.pagehelper.PageInfo;
import com.project.bean.MessageBean;
import com.project.service.IMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;

@Controller
@RequestMapping("thymeleaf")
public class ThymeleafController {
    @Autowired
    private IMessageService service;

    @RequestMapping("findByItem")
    public String findByItem(
            @RequestParam(value = "pageNo",defaultValue = "1") int pageNo,
            String name, HttpServletRequest request){
        PageInfo<MessageBean> info = service.findByItem(pageNo,name);
        request.setAttribute("name",name);
        request.setAttribute("pageInfo",info);
        return "/html/message/message.html";
    }

    @RequestMapping("/add")
    public String add(MessageBean message){
        service.add(message);
        return "redirect:/thymeleaf/findByItem";
    }

    @RequestMapping("del")
    public String del(int id){
        service.del(id);
        return "redirect:/thymeleaf/findByItem";
    }

    @RequestMapping("findById")
    public String findById(int id,HttpServletRequest request){
        //调用业务方法,查询商品
        MessageBean message = service.findById(id);
        request.setAttribute("message",message);
        return "/html/message/update.html";
    }

    @RequestMapping("update")
    public String update(int id,String phone){
        service.update(id,phone);
        return "redirect:/thymeleaf/findByItem";
    }
}
